package com.car.loan.project.cm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.controller.BaseController;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.cm.domain.CmInsuranceCompany;
import com.car.loan.project.cm.service.ICmInsuranceCompanyService;
import com.car.loan.project.cm.util.bo.InsuranceBaseBo;
import com.car.loan.project.cm.util.enunms.DealerStatus;
import com.car.loan.project.cm.util.select.InsuranceBaseSelect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2019/10/22 19:36
 */
@RestController
@RequestMapping("/cm/insurance")
@Api(value = "保险公司管理", tags = "基础数据-保险公司管理接口")
public class CmInsuranceController extends BaseController {

    @Autowired
    private ICmInsuranceCompanyService insuranceCompanyService;

    @PreAuthorize("@ss.hasPermi('cm:insurance:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取保险公司列表", responseContainer = "list", response = CmInsuranceCompany.class)
    public AjaxResult list(@Valid InsuranceBaseSelect insuranceBaseSelect) {
        IPage<CmInsuranceCompany> page = new Page<>(insuranceBaseSelect.getPageNum(),
            insuranceBaseSelect.getPageSize());
        CmInsuranceCompany cmInsuranceCompany = new CmInsuranceCompany();
        if (null != insuranceBaseSelect.getStatus()) {
            cmInsuranceCompany.setStatus(insuranceBaseSelect.getStatus());
        }
        QueryWrapper<CmInsuranceCompany> queryWrapper = new QueryWrapper<>(cmInsuranceCompany);
        if (StringUtils.isNotEmpty(insuranceBaseSelect.getName())) {
            queryWrapper.like("name", insuranceBaseSelect.getName());
        }
        queryWrapper.orderByDesc("id");
        return AjaxResult.success(insuranceCompanyService.selectPage(page, queryWrapper));
    }


    @PreAuthorize("@ss.hasPermi('cm:insurance:query')")
    @GetMapping(value = "/{insuranceId}")
    @ApiOperation(value = "获取保险公司详情", response = CmInsuranceCompany.class)
    public AjaxResult getInfo(@PathVariable Integer insuranceId) {
        return AjaxResult
            .success(insuranceCompanyService.selectCmInsuranceCompanyById(insuranceId));
    }


    @PreAuthorize("@ss.hasPermi('cm:insurance:add')")
    @Log(title = "保险公司管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加保险公司")
    public AjaxResult add(
        @ApiParam(value = "名称", required = true) @RequestParam("name") String name,
        @ApiParam(value = "简称", required = true) @RequestParam("alias") String alias) {
        CmInsuranceCompany insuranceCompany = new CmInsuranceCompany();
        insuranceCompany.setName(name);
        insuranceCompany.setAlias(alias);
        insuranceCompany.setStatus(DealerStatus.NORMAL.status);
        return toAjax(insuranceCompanyService.insertCmInsuranceCompany(insuranceCompany));
    }

    @PreAuthorize("@ss.hasPermi('cm:insurance:edit')")
    @Log(title = "保险公司管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改保险公司")
    public AjaxResult edit(@RequestBody @Valid InsuranceBaseBo insuranceBaseBo) {

        CmInsuranceCompany cmDealer = new CmInsuranceCompany();
        cmDealer.setId(insuranceBaseBo.getId());
        cmDealer.setName(insuranceBaseBo.getName());
        cmDealer.setAlias(insuranceBaseBo.getAlias());
        cmDealer.setStatus(insuranceBaseBo.getStatus());
        insuranceCompanyService.updateCmInsuranceCompany(cmDealer);
        return AjaxResult.success();
    }
}
